import 'package:casdon/app/config/env/common_config.dart';
import 'package:casdon/app/config/theme/theme_extention.dart';
import 'package:casdon/common/widget/primary_check_box.dart';
import 'package:casdon/auto_router.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';

class AgreementCheckBox extends StatefulWidget {
  final bool value;
  final Function(bool) onChanged;
  final bool? isRegister;

  const AgreementCheckBox({
    super.key,
    required this.value,
    required this.onChanged,
    this.isRegister = false,
  });

  @override
  State<AgreementCheckBox> createState() => _AgreementCheckBoxState();
}

class _AgreementCheckBoxState extends State<AgreementCheckBox> {
  late bool _checked;

  @override
  void initState() {
    super.initState();
    _checked = widget.value;
  }

  void _toggle(bool? value) {
    setState(() {
      _checked = value ?? false;
    });
    widget.onChanged(_checked);
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Align(
          alignment: Alignment.topCenter,
          child: SizedBox(
            width: 28,
            height: 28,
            child: PrimaryCheckBox(value: _checked, onChanged: _toggle),
          ),
        ),
        Expanded(
          child: RichText(
            text: TextSpan(
              style: context.textTheme.bodyMedium?.copyWith(
                color: context.customColorThemeNonNull.textColor2,
              ),
              children: [
                const TextSpan(text: '已阅读并同意 '),
                TextSpan(
                  text: '《用户协议》',
                  style: context.textTheme.bodyMedium?.copyWith(
                    color: context.colorScheme.primary,
                  ),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      WebPageRoute(
                        url: CommonConfig.urser_protocol,
                        title: "《用户协议》",
                      ).push(context);
                    },
                ),
                const TextSpan(text: ' 和 '),
                TextSpan(
                  text: '《隐私政策》',
                  style: TextStyle(color: context.colorScheme.primary),
                  recognizer: TapGestureRecognizer()
                    ..onTap = () {
                      WebPageRoute(
                        url: CommonConfig.privacy_protocol,
                        title: "《隐私政策》",
                      ).push(context);
                    },
                ),
                if (widget.isRegister == false)
                  const TextSpan(text: '，未注册的手机号/邮箱将自动创建凯度账号'),
              ],
            ),
          ),
        ),
      ],
    );
  }

  @override
  void didUpdateWidget(covariant AgreementCheckBox oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.value != widget.value) {
      setState(() {
        _checked = widget.value;
      });
    }
  }
}
